using System;
using EPiServer.Common;
using EPiServer.Common.Queries;
using EPiServer.Common.Security;
using EPiServer.Common.Tags;

namespace EPiAbstractions.Common.Tags
{
    public class TagHandlerFacade : ITagHandlerFacade
    {
        private static TagHandlerFacade _instance;

        public TagHandlerFacade()
        {
            TagHandler.TagAdded += OnTagAdded;
            TagHandler.TagPredefined += OnTagPredefined;
            TagHandler.TagRemoved += OnTagRemoved;
            TagHandler.TagPredefinitionRemoved += OnTagPredefinitionRemoved;
            TagHandler.EntityTagAdded += OnEntityTagAdded;
            TagHandler.EntityTagRemoved += OnEntityTagRemoved;
        }

        public static TagHandlerFacade Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new TagHandlerFacade();

                return _instance;
            }

            set { _instance = value; }
        }

        #region ITagHandlerFacade Members

        public event EPiServerCommonEventHandler TagAdded;
        public event TagPredefinitionEventHandler TagPredefined;
        public event EPiServerCommonEventHandler TagRemoved;
        public event TagPredefinitionEventHandler TagPredefinitionRemoved;
        public event EPiServerCommonEventHandler EntityTagAdded;
        public event EPiServerCommonEventHandler EntityTagRemoved;

        public virtual Tag AddTag(Tag tag)
        {
            return TagHandler.AddTag(tag);
        }

        public virtual void RemoveTag(Tag tag)
        {
            TagHandler.RemoveTag(tag);
        }

        public virtual Tag GetTag(String name)
        {
            return TagHandler.GetTag(name);
        }

        public virtual Tag GetTag(Int32 id)
        {
            return TagHandler.GetTag(id);
        }

        public virtual TagCollection GetTags(Type type, ISite site, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return TagHandler.GetTags(type, site, page, pageSize, out totalItems);
        }

        public virtual TagCollection GetTags(Type type, ISite site, IUser user, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return TagHandler.GetTags(type, site, user, page, pageSize, out totalItems);
        }

        public virtual TagCollection GetMostPopularTags(Type type, ISite site, Boolean siteIsGlobal, String searchFor, WildCardType wildCard,
                                                        Int32 maxItems)
        {
            return TagHandler.GetMostPopularTags(type, site, siteIsGlobal, searchFor, wildCard, maxItems);
        }

        public virtual IPredefinedTag AddPredefinedTag(IPredefinedTag predefinedTag)
        {
            return TagHandler.AddPredefinedTag(predefinedTag);
        }

        public virtual void RemovePredefinedTag(IPredefinedTag predefinedTag)
        {
            TagHandler.RemovePredefinedTag(predefinedTag);
        }

        public virtual PredefinedTagCollection GetPredefinedTags()
        {
            return TagHandler.GetPredefinedTags();
        }

        public virtual PredefinedTagCollection GetPredefinedTags(Type type, ISite site)
        {
            return TagHandler.GetPredefinedTags(type, site);
        }

        public virtual PredefinedTagCollection GetPredefinedTags(Type type, IUser user)
        {
            return TagHandler.GetPredefinedTags(type, user);
        }

        public virtual PredefinedTagCollection GetPredefinedTags(Type type, ISite site, IUser user)
        {
            return TagHandler.GetPredefinedTags(type, site, user);
        }

        public virtual TagCloud GetTagCloud()
        {
            return TagHandler.GetTagCloud();
        }

        public virtual TagCloud GetTagCloud(Int32 numItems)
        {
            return TagHandler.GetTagCloud(numItems);
        }

        public virtual TagCloud GetTagCloud(Type type, ISite site, DateTime since)
        {
            return TagHandler.GetTagCloud(type, site, since);
        }

        public virtual TagCloud GetTagCloud(ITaggableEntity e, DateTime since, Int32 numItems)
        {
            return TagHandler.GetTagCloud(e, since, numItems);
        }

        public virtual TagCloud GetTagCloud(ITaggableEntity e, IUser user, Int32 numItems)
        {
            return TagHandler.GetTagCloud(e, user, numItems);
        }

        public virtual TagCloud GetTagCloud(Type type, ISite site, DateTime since, Int32 numItems)
        {
            return TagHandler.GetTagCloud(type, site, since, numItems);
        }

        public virtual TagCloud GetTagCloud(Type type, ISite site, IUser user, Int32 numItems)
        {
            return TagHandler.GetTagCloud(type, site, user, numItems);
        }

        public virtual TagCloud GetTagCloud(Type type, IUser user)
        {
            return TagHandler.GetTagCloud(type, user);
        }

        public virtual TagCloud GetTagCloud(Type type, IUser user, Int32 numItems)
        {
            return TagHandler.GetTagCloud(type, user, numItems);
        }

        public virtual TagCloud GetTagCloud(IUser user)
        {
            return TagHandler.GetTagCloud(user);
        }

        public virtual TagCloud GetTagCloud(IUser user, Int32 numItems)
        {
            return TagHandler.GetTagCloud(user, numItems);
        }

        #endregion

        public virtual void OnTagAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (TagAdded != null)
                TagAdded(sender, e);
        }

        public virtual void OnTagPredefined(String sender, TagPredefinitionEventArgs eventArgs)
        {
            if (TagPredefined != null)
                TagPredefined(sender, eventArgs);
        }

        public virtual void OnTagRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (TagRemoved != null)
                TagRemoved(sender, e);
        }

        public virtual void OnTagPredefinitionRemoved(String sender, TagPredefinitionEventArgs eventArgs)
        {
            if (TagPredefinitionRemoved != null)
                TagPredefinitionRemoved(sender, eventArgs);
        }

        public virtual void OnEntityTagAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (EntityTagAdded != null)
                EntityTagAdded(sender, e);
        }

        public virtual void OnEntityTagRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (EntityTagRemoved != null)
                EntityTagRemoved(sender, e);
        }
    }
}